﻿package org.soretro.portfolio.view.background
{
	import away3d.cameras.*;
	import away3d.cameras.lenses.PerspectiveLens;
	import away3d.containers.*;
	import away3d.controllers.HoverController;
	import away3d.core.base.*;
	import away3d.debug.*;
	import away3d.entities.Mesh;
	import away3d.events.*;
	import away3d.filters.BloomFilter3D;
	import away3d.filters.BlurFilter3D;
	import away3d.filters.DepthOfFieldFilter3D;
	import away3d.filters.MotionBlurFilter3D;
	import away3d.library.AssetLibrary;
	import away3d.library.assets.AssetType;
	import away3d.lights.DirectionalLight;
	import away3d.lights.PointLight;
	import away3d.loaders.*;
	import away3d.loaders.misc.AssetLoaderContext;
	import away3d.loaders.parsers.*;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.ColorMaterial;
	import away3d.materials.methods.FilteredShadowMapMethod;
	import away3d.primitives.Cube;
	import away3d.primitives.Plane;
	import away3d.primitives.WireframeSphere;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.BlurFilter;
	import flash.geom.Vector3D;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.utils.getTimer;
	
	import mx.core.UIComponent;
	import mx.managers.SystemManager;
	
	import spark.filters.BlurFilter;
	
	[SWF(backgroundColor="#000000", frameRate="12", quality="LOW")]
	
	public class BasicAnim extends UIComponent
	{
		//signature swf
//		[Embed(source="/../embeds/signature.swf", symbol="Signature")]
//		public var SignatureSwf:Class;
		
		//solider ant texture
//		[Embed(source="/../embeds/soldier_ant.jpg")]
//		private var AntTexture:Class;
		
		//solider ant model
//		[Embed(source="/../embeds/soldier_ant.3ds",mimeType="application/octet-stream")]
//		private var AntModel:Class;
		
//		[Embed(source="/",mimeType="application/octet-stream")]
//		private var RaceTrack:Class;
		
		//ground texture
		[Embed(source="./assets/facebook_logo_big.png")]
		private var SandTexture:Class;
		
		//engine variables
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var view:View3D;
		private var cameraController:HoverController;
		
		//signature variables
//		private var Signature:Sprite;
//		private var SignatureBitmap:Bitmap;
		
		//material objects
//		private var groundMaterial:BitmapMaterial;
		
		//scene objects
		private var light:DirectionalLight;
		private var direction:Vector3D;
		private var loader:Loader3D;
//		private var ground:Plane;
		
		//navigation variables
		private var moving:Boolean = false;
		private var lastPanAngle:Number;
		private var lastTiltAngle:Number;
		private var lastMouseX:Number;
		private var lastMouseY:Number;

		private var sphere:WireframeSphere;

		private var camDirection:int;
		
		/**
		 * Constructor
		 */
		public function BasicAnim( )
		{
			addEventListener(Event.ADDED_TO_STAGE, init );
		}
		
		/**
		 * Global initialise function
		 */
		private function init( event:Event = null ):void
		{
			Parsers.enableAllBundled();
			
			initEngine();
			initObjects();
			initListeners();
			
//			alpha = 0.2;
			
//			view.filters3d = [
//				new BlurFilter3D(9,9),
//				new MotionBlurFilter3D(.8),
//				new DepthOfFieldFilter3D(6,6),
//			];
		}
		
		/**
		 * Initialise the engine
		 */
		private function initEngine():void
		{
//			stage.scaleMode = StageScaleMode.NO_SCALE;
//			stage.align = StageAlign.TOP_LEFT;
			view = new View3D();
//			view.backgroundColor = 0xFF00FF;
//			view.scene = scene;
//			view.camera = camera;
			
			//setup camera for optimal shadow rendering
//			camera = new Camera3D();
			camera = view.camera;
			camera.lens = new PerspectiveLens();
//			camera.x = 500;
//			camera.y = 1;
//			camera.z = 500;
//			camera.lookAt( new Vector3D(0,0,0));
			camera.lens.near = 200;
			camera.lens.far = 3000;
			
			addChild(view);
//			addChild(new AwayStats(view));
			
			sphere = new WireframeSphere( 700, 6, 6, 0xEEEEEE, 3);
			
			sphere.x = 0;
			sphere.y = 80;
			sphere.z = 0;
			view.scene.addChild(sphere);
			
//			scene = new Scene3D();
			
			
			//setup controller to be used on the camera
			cameraController = new HoverController(camera, null, 45, 20, 1000, 10);
			
			
			//add signature
//			Signature = Sprite(new SignatureSwf());
//			SignatureBitmap = new Bitmap(new BitmapData(Signature.width, Signature.height, true, 0));
//			stage.quality = StageQuality.HIGH;
//			SignatureBitmap.bitmapData.draw(Signature);
//			stage.quality = StageQuality.LOW;
//			addChild(SignatureBitmap);
			
		}
		
		/**
		 * Initialise the scene objects
		 */
		private function initObjects():void
		{
			
			light = new DirectionalLight(-1, -1, 1);
//			direction = new Vector3D(-1, -1, 1);
			
			view.scene.addChild(light);
			
			view.backgroundColor = 0xE3E3E3;
//			view.backgroundAlpha = 0.3;
			
			var assetLoaderContext:AssetLoaderContext = new AssetLoaderContext();
//			assetLoaderContext.mapUrlToData("texture.jpg", new AntTexture());
			
			loader = new Loader3D();
			loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);
			loader.scale(70);
			
//			loader.z = 200;
//			loader.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
//			loader.loadData(new RaceTrack(), new Parsers(), assetLoaderContext);
			loader.load( new URLRequest('./assets/arcade_machines_navi.obj') );
			
//			groundMaterial = new BitmapMaterial((new SandTexture()).bitmapData);
//			groundMaterial.lights = [light];
//			groundMaterial.specular = 0;
//			groundMaterial.alpha = 0.1;
//			groundMaterial.shadowMethod = new FilteredShadowMapMethod(light);
//			ground = new Plane(groundMaterial, 500, 500);
			
//			var cube:Cube = new Cube();
//			cube.material = groundMaterial;
//			cube.width = 300;
//			cube.height = 300;
//			cube.z = -400;
			
//			view.scene.addChild(ground);
			view.scene.addChild(loader);
//			view.scene.addChild(cube);
		}
		
		private function onResourceComplete(event : LoaderEvent) : void
		{
//			var mesh : Mesh;
//			for (var i : int = 0; i < loader.numChildren; ++i) {
//				mesh = Mesh(loader.getChildAt(i));
//				mesh.geometry.scale(100);
//				mesh.y = -70
//				//mesh.material = _material
//			}
		}
		
		/**
		 * Initialise the listeners
		 */
		private function initListeners():void
		{
//			addEventListener(Event.ENTER_FRAME, onEnterFrame);
//			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
//			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			addEventListener(Event.RESIZE, onResize);
			onResize();
		}
		
		public function startAnim():void
		{
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		/**
		 * Navigation and render loop
		 */
		private function onEnterFrame(event:Event):void
		{
//			if (true) {
//				cameraController.panAngle = 01.3 * (stage.mouseX - lastMouseX) + lastPanAngle;
//				cameraController.tiltAngle = 0.3 * (stage.mouseY - lastMouseY) + lastTiltAngle;
//				cameraController.panAngle = 0.3 * 1 + lastPanAngle;
//				cameraController.tiltAngle = 0.3 * 1 + lastTiltAngle;
//			}
			
//			ground.rotationY -= .04;
//			ground.rotationZ -= 0.3;
			
			loader.rotationY -= .04;
			
			sphere.rotationZ += .04;
			sphere.rotationY += .01;
			
			if (camera.z > 2000) {
				camDirection = -.4;
			} else if ( camera.z < 500 ) {
				camDirection = .4;
			}
			
			camera.z += camDirection;
			camera.lookAt( new Vector3D(0,0,0) );
			
//			direction.x = -Math.sin(getTimer()/4000);
//			direction.z = -Math.cos(getTimer()/4000);
//			light.direction = direction;
			
			if(this.view && this.view.stage)
			{
				view.render();
			}
		}
		
		/**
		 * Listener function for asset complete event on loader
		 */
		private function onAssetComplete(event:AssetEvent):void
		{
//			if (event.asset.assetType == AssetType.MESH) {
//				var mesh:Mesh = event.asset as Mesh;
//				mesh.castsShadows = true;
//			} else if (event.asset.assetType == AssetType.MATERIAL) {
//				var material:BitmapMaterial = event.asset as BitmapMaterial;
//				material.shadowMethod = new FilteredShadowMapMethod(light);
//				material.lights = [light];
//				material.gloss = 130;
//				material.specular = 1;
//				material.ambientColor = 0xFF0000;
//				material.ambient = 1;
//			}
		}
		
		/**
		 * Mouse down listener for navigation
		 */
		private function onMouseDown(event:MouseEvent):void
		{
			lastPanAngle = cameraController.panAngle;
			lastTiltAngle = cameraController.tiltAngle;
			lastMouseX = stage.mouseX;
			lastMouseY = stage.mouseY;
			moving = true;
			stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
		
		/**
		 * Mouse up listener for navigation
		 */
		private function onMouseUp(event:MouseEvent):void
		{
			moving = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
		
		/**
		 * Mouse stage leave listener for navigation
		 */
		private function onStageMouseLeave(event:Event):void
		{
			moving = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
		
		/**
		 * stage listener for resize events
		 */
		private function onResize(event:Event = null):void
		{
//			view.x = 30;
//			view.y = 30;
			view.width = SystemManager.getSWFRoot(this).width;
			view.height = SystemManager.getSWFRoot(this).height;
//			SignatureBitmap.y = SystemManager.getSWFRoot(this).height - Signature.height;
		}
	}
}